package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.ICT;
import com.nl.controller.web.bean.ICTTable;
import com.nl.controller.web.mapper.IctMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class IctService {
    @Autowired
    private IctMapper ictMapper;
    @Autowired
    private CommonService commonService;


    @SystemServiceLog(description = "周报数据获取", value = false)
    public ICTTable getDataList() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);  //当前年份
        ICTTable ictTable = new ICTTable();
        List<String> titleList = new ArrayList<>();
        List<ICT> dataList;
        String week;      
        try {
            dataList = ictMapper.findMgr();      //获取项目列表以及本周的数据
            ICT ict = new ICT();
            ict.setMgrName("全年度累计");
            ict.setCityName("江苏");
            ict.setWeekData(ictMapper.findAllpalce("0"));
            ict.setNowMonthData(ictMapper.findAllpalce("1"));
            ict.setCreateTime(" ");
            dataList.add(ict);
            titleList.add("序号");
            titleList.add("地市名称");
            titleList.add("区县名称");
            titleList.add("项目名称");
            titleList.add("开通日期");
            titleList.add("本周");
           /* String title = String.valueOf(year - 1) + "全年";
            titleList.add(title);*/
            List<String> sl = this.getMonth();
            titleList.addAll(sl);   
            titleList.add("本月");
            titleList.add("本年合计");
            titleList.add("地市本月合计");
            titleList.add("地市本年合计");
            titleList.add("地市名称");  
            for (ICT a : dataList) {
            	int mgrYear = 0;//项目本年
                int cityYear= 0;//地市本年
                a.setCityName2(a.getCityName());
                a.setCountyName(changeNull1(a.getCountyName()));
                a.setWeekData(changeNull(a.getWeekData()));
                a.setNowMonthData(changeNull(a.getNowMonthData()));             
                List<String> monthList = new ArrayList<>();//项目的数据源列表
                for (String month : sl) {
                    String monthData = ictMapper.findMonthData(this.getMsg(a.getMgrId(), month, "0",""));//今年每月汇总
                    monthList.add(changeNull(monthData));
                    if(monthData==null)
                    {
                    	monthData ="0";                        
                    }
                    mgrYear = mgrYear +Integer.parseInt(monthData);
                    	
                    String cityMonthData = ictMapper.findMonthData(this.getMsg("", month, "0",a.getCityId()));
                    if(cityMonthData==null)
                    {
                    	cityMonthData ="0";                        
                    }
                    cityYear =  cityYear + Integer.parseInt(cityMonthData);                 	
                }
                a.setMonthData(monthList);
                a.setNowYearData(changeNull2(changeNull(String.valueOf(mgrYear+Integer.parseInt(changeNull3(a.getNowMonthData()))))));
                a.setNowCityMonthData(changeNull2(changeNull(ictMapper.findAllpalceNowCity(a.getCityId()))));
                a.setNowCityYearData(changeNull2(changeNull(String.valueOf(cityYear+Integer.parseInt(changeNull3(a.getNowCityMonthData()))))));
            }
            (dataList.get(dataList.size()-1)).setNowCityMonthData(changeNull(ictMapper.findAllpalce("1")));
            (dataList.get(dataList.size()-1)).setNowCityYearData((dataList.get(dataList.size()-1)).getNowYearData());
            ictTable.setTitleList(titleList);
            ictTable.setDataList(dataList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("周报统计异常");
            throw new SmsException("周报统计出现异常");
        }

        return ictTable;
    }

    /**
     * 查询条件生成
     *
     * @param mgrId 项目编号
     * @param month 月份编号
     * @param mark  汇总周期标记，0每月 1上半年 2下半年 3当年累计
     * @param cityId 
     * @return
     */
    private Map<String, Object> getMsg(String mgrId, String month, String mark, String cityId) {
        Map<String, Object> map = new HashMap<>();
        map.put("mgrId", mgrId);
        map.put("month", month);
        map.put("mark", mark);
        map.put("cityId", cityId);
        return map;
    }

    /**
     * 时间列表展示
     *
     * @return
     */
    private List getMonth() {
        List<String> dateList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);  //当前年份
        int month = cal.get(Calendar.MONTH) + 1;//当前月份
        for (int i = 1; i < month; i++) {
            StringBuffer sbf = new StringBuffer();
            sbf = sbf.append(year);
            sbf = sbf.append(i > 9 ? String.valueOf(i) : ("0" + i));
            dateList.add(sbf.toString());
        }
        return dateList;
    }


    /**
     * 为空的处理
     */
    private String changeNull(String parm) {
        if (parm == null||"".equals(parm)) {
            return "无";
        } else {
            return parm;
        }
    }
    
    private String changeNull1(String parm) {
        if (parm == null||"".equals(parm)) {
            return "";
        } else {
            return parm;
        }
    }
    
    private String changeNull2(String parm) {
        if ("0".equals(parm)) {
            return "无";
        } else {
            return parm;
        }
    }
    
    private String changeNull3(String parm) {
        if ("无".equals(parm)) {
            return "0";
        } else {
            return parm;
        }
    }
    
    public int findEndDate(Map<String, String> map){
    	String endDate = ictMapper.findEndDate();
    	if (map.containsKey(endDate)) {
			log.info("表内最后时间："+endDate);
			return -1;
		}else{
    	map.put(endDate,endDate);
    	return 1;
		}
    }
}



